package com.hunan.api.catv.mq.consumer;

import com.google.gson.JsonObject;
import com.hunan.api.catv.common.Constants;
import com.hunan.api.catv.mq.MqConstants;
import com.hunan.api.catv.service.biz.toupiao.TouPiaoBiz;
import com.hunan.api.catv.utils.GsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ToupiaoCosumer {

    public static Logger log = LoggerFactory.getLogger(ToupiaoCosumer.class);

    @Autowired
    TouPiaoBiz touPiaoBiz;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConstants.toupiao_queue, durable = "true"), exchange = @Exchange(value = MqConstants.toupiao_exchange, durable = "true", type = "topic"), key = MqConstants.toupiao_routingkey))
    public void tasks(String msg) {
        log.info("[加票收到任务消息:{}]", msg);
        JsonObject msgJson = GsonUtils.fromJson(msg, JsonObject.class);
        Integer type = msgJson.get("type").getAsInt();
        Integer memberId = msgJson.get("memberId").getAsInt();
        try {
            touPiaoBiz.addTicket(memberId, type, Constants.toupiao_conf);
        } catch (Exception e) {
            log.error("[用户加票失败:{}]", e);
        }
    }
}
